<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\DB;
/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $modelValidate = true;
    public function initialize()
    {
        parent::initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->withJoin('group','left')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                    ->withJoin('group','left')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            foreach ($list as $k => $v)
            {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * add
     */
    public function add()
    {
        if (!$this->request->isPost()) {
            $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), '', ['class' => 'form-control selectpicker']));
        }
        return parent::add();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $list = DB::name('user_info')->where('user_id', 'in', $ids)->select();
            if($list){
                $this->error('客户信息配置菜单中客户信息已添加此用户请先删除客户信息');
            }
            $list = DB::name('user_config')->where('user_id', 'in', $ids)->select();
            if($list){
                $this->error('客户信息配置菜单中客户配置已添加此用户请先删除客户配置');
            }
            $list = DB::name('user_app_business')->where('user_id', 'in', $ids)->select();
            if($list){
                $this->error('客户信息配置菜单中App业务测试已添加此用户请先删除App业务测试');
            }
            $list = DB::name('user_task')->where('user_id', 'in', $ids)->select();
            if($list){
                $this->error('客户信息配置菜单中故障日志任务配置已添加此用户请先删除故障日志任务配置');
            }
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v) {
                $count += $v->delete();
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
